﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SobFS.Common;

namespace SobFS.Web.backend
{
    public partial class sign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //获取后台Cookie登录名
                this.txtLoginName.Text = CookieHelper.ReaderCookie(SobFSConst.COOKIE_BACKEND_REMEMBERUSERNAME);
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void IbtnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string loginInfo = this.txtLoginName.Text.Trim();
            string password = this.txtPassword.Text.Trim();
            string captcha = this.txtCaptcha.Text.Trim();

            if (string.IsNullOrWhiteSpace(loginInfo))
            {
                this.SetFocus(this.txtLoginName);
                this.lblMessage.Text = "请输入用户名！";
                return;
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                this.SetFocus(this.txtPassword);
                this.lblMessage.Text = "请输入登录密码！";
                return;
            }
            if (string.IsNullOrWhiteSpace(captcha))
            {
                this.SetFocus(this.txtCaptcha);
                this.lblMessage.Text = "请输入验证码！";
                return;
            }
            if (Session[SobFSConst.SESSION_CAPTCHA] == null)
            {
                this.SetFocus(this.txtCaptcha);
                this.lblMessage.Text = "验证码失效！";
                return;
            }

            this.txtCaptcha.Text = Session[SobFSConst.SESSION_CAPTCHA].ToString();
            captcha = this.txtCaptcha.Text.Trim();

            if (captcha.ToLower() != Session[SobFSConst.SESSION_CAPTCHA].ToString().ToLower())
            {
                this.SetFocus(this.txtCaptcha);
                this.lblMessage.Text = "验证码输入不正确！";
                return;
            }

            //TODO 登录验证、获取权限
            Model.SysUser model;
            bool result = new BLL.SysUser().Sign(loginInfo, password, out model);
            if (result)
            {
                Session[SobFSConst.SESSION_BACKEND_USER] = model;
                //TODO 写入登录日志

                //写入Cookies
                if (this.chkRemember.Checked)
                {
                    CookieHelper.WriterCookie(SobFSConst.COOKIE_BACKEND_REMEMBERUSERNAME, loginInfo, 60 * 60 * 24 * 2);
                }
                else
                {
                    CookieHelper.WriterCookie(SobFSConst.COOKIE_BACKEND_REMEMBERUSERNAME, loginInfo, -(60 * 60 * 24 * 2));
                }

                CookieHelper.WriterCookie(SobFSConst.COOKIE_BACKEND_USERNAME, SobFSConst.KEY_PASSWORD, loginInfo);
                CookieHelper.WriterCookie(SobFSConst.COOKIE_BACKEND_USERNAME, SobFSConst.KEY_PASSWORD, password);
                Response.Redirect("index.aspx");
            }
            else
            {
                this.SetFocus(this.txtLoginName);
                this.lblMessage.Text = "登录失败！";
            }
        }

    }
}